package com.aiju.cash.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.view.View;

import com.aiju.app.config.ConfigManager;
import com.aiju.utils.L;
import com.aiju.widgit.dialog.ListDialog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import static com.aiju.cash.ui.good.GoodAddActivity.REQUEST_CODE;

/**
 * @author: 佐为
 * @date: 2016-12-26 16:34
 * @version: V1.0
 * @Description: 描述
 */
public class ImageDialogUtil {

    public static int REQUEST_CAMERA_1 = 1;

    public static List<String> options = new ArrayList<>();

    static {
        options.add("拍照");
        options.add("从手机相册选择");
    }

    public static Bitmap saveCode(View view) {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
        bitmap = bitmap.createBitmap(bitmap);
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }

    public static void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "Aiiju/QRCode");
        if (!appDir.exists()) {
            appDir.getParentFile().mkdirs();
            appDir.mkdir();
        }
        String fileName = "code" + ".jpg";
        File file = new File(appDir, fileName);
        L.d(file.getAbsolutePath());
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 显示选择图片对话框
     */
    public static void showSelectPicDialog(final Activity activity) {
        showSelectPicDialog(activity, new ListDialog.OnItemOnClick() {
            @Override
            public void click(String item, int position) {
                if (item.equals("拍照")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机
                    activity.startActivityForResult(intent, ImageDialogUtil.REQUEST_CAMERA_1);
                } else {
                    new ImageSelectorHelper().openLogoImageSelector(activity, REQUEST_CODE);
                }
            }
        });
    }
    /**
     * 显示选择图片对话框
     */
    public static void showSelectPicDialogByRequetCode(final Activity activity,final int requestCode) {
        showSelectPicDialog(activity, new ListDialog.OnItemOnClick() {
            @Override
            public void click(String item, int position) {
                if (item.equals("拍照")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机
                    activity.startActivityForResult(intent, requestCode);
                } else {
                    new ImageSelectorHelper().openLogoImageSelector(activity, requestCode);
                }
            }
        });
    }

    /**
     * 显示选择图片对话框
     */
    public static void showSelectPicDialog(final Activity activity, ListDialog.OnItemOnClick onClick) {
        new ListDialog(activity)
                .setLayoutParams()
                .setData(options)
                .setOnItemOnClick(onClick)
                .show();
    }

    /**
     * 保存相机的图片
     **/
    public static String saveCameraImage(Intent data) {
        // 检查sd card是否存在
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || data.getExtras() == null) {
            return "";
        }
        // 为图片命名啊
        String name = new DateFormat().format("yyyymmdd", Calendar.getInstance(Locale.CHINA)) + ".jpg";
        Bitmap bmp = (Bitmap) data.getExtras().get("data");// 解析返回的图片成bitmap

        // 保存文件
        FileOutputStream fos = null;
        String fileName = ConfigManager.DIR_IMAGE + name;// 保存路径

        try {// 写入SD card
            fos = new FileOutputStream(fileName);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }

}
